package dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Address entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.Address
 * @author MyEclipse Persistence Tools
 */

public class AddressDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(AddressDAO.class);
	// property constants
	public static final String ADDRLEVEL = "addrlevel";
	public static final String ABBREVIATION = "abbreviation";
	public static final String LEVEL1CN = "level1cn";
	public static final String LEVEL2CN = "level2cn";
	public static final String LEVEL3CN = "level3cn";
	public static final String LEVEL4CN = "level4cn";
	public static final String LEVEL5CN = "level5cn";
	public static final String FULLCNINFO = "fullcninfo";
	public static final String LEVEL1EN = "level1en";
	public static final String LEVEL2EN = "level2en";
	public static final String LEVEL3EN = "level3en";
	public static final String LEVEL4EN = "level4en";
	public static final String LEVEL5EN = "level5en";
	public static final String FULLENINFO = "fulleninfo";
	public static final String YUNSHUMODE = "yunshumode";
	public static final String ADDRSTATUS = "addrstatus";

	protected void initDao() {
		// do nothing
	}

	public void save(Address transientInstance) {
		log.debug("saving Address instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Address persistentInstance) {
		log.debug("deleting Address instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Address findById(java.lang.Integer id) {
		log.debug("getting Address instance with id: " + id);
		try {
			Address instance = (Address) getHibernateTemplate().get(
					"dao.Address", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Address instance) {
		log.debug("finding Address instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Address instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = " from Address as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findProvinceAddress(String province) {
		try {
			String queryString = " from Address as model where model.addrlevel=2 and model.level1cn= ?";
			return getHibernateTemplate().find(queryString, province);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findCityAddress(String city) {
		try {
			String queryString = " from Address as model where model.addrlevel=3 and model.level2cn= ?";
			return getHibernateTemplate().find(queryString, city);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findAreaAddress(String area) {
		try {
			String queryString = " from Address as model where model.addrlevel=4 and model.level3cn= ?";
			return getHibernateTemplate().find(queryString, area);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findStreetAddress(String street) {
		try {
			String queryString = " from Address as model where model.addrlevel=5 and model.level4cn= ?";
			return getHibernateTemplate().find(queryString, street);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAddrlevel(Object addrlevel) {
		return findByProperty(ADDRLEVEL, addrlevel);
	}

	public List findByAbbreviation(Object abbreviation) {
		return findByProperty(ABBREVIATION, abbreviation);
	}

	public List findByLevel1cn(Object level1cn) {
		return findByProperty(LEVEL1CN, level1cn);
	}

	public List findByLevel2cn(Object level2cn) {
		return findByProperty(LEVEL2CN, level2cn);
	}

	public List findByLevel3cn(Object level3cn) {
		return findByProperty(LEVEL3CN, level3cn);
	}

	public List findByLevel4cn(Object level4cn) {
		return findByProperty(LEVEL4CN, level4cn);
	}

	public List findByLevel5cn(Object level5cn) {
		return findByProperty(LEVEL5CN, level5cn);
	}

	public List findByFullcninfo(Object fullcninfo) {
		return findByProperty(FULLCNINFO, fullcninfo);
	}

	public List findByLevel1en(Object level1en) {
		return findByProperty(LEVEL1EN, level1en);
	}

	public List findByLevel2en(Object level2en) {
		return findByProperty(LEVEL2EN, level2en);
	}

	public List findByLevel3en(Object level3en) {
		return findByProperty(LEVEL3EN, level3en);
	}

	public List findByLevel4en(Object level4en) {
		return findByProperty(LEVEL4EN, level4en);
	}

	public List findByLevel5en(Object level5en) {
		return findByProperty(LEVEL5EN, level5en);
	}

	public List findByFulleninfo(Object fulleninfo) {
		return findByProperty(FULLENINFO, fulleninfo);
	}

	public List findByYunshumode(Object yunshumode) {
		return findByProperty(YUNSHUMODE, yunshumode);
	}

	public List findByAddrstatus(Object addrstatus) {
		return findByProperty(ADDRSTATUS, addrstatus);
	}

	public List findAll() {
		log.debug("finding all Address instances");
		try {
			System.out.println("findAll");
			String queryString = "from Address";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Address merge(Address detachedInstance) {
		log.debug("merging Address instance");
		try {
			Address result = (Address) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Address instance) {
		log.debug("attaching dirty Address instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Address instance) {
		log.debug("attaching clean Address instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AddressDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AddressDAO) ctx.getBean("AddressDAO");
	}
}